package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsUserMapper;
import com.qf.mapper.StatMapper;
import com.qf.pojo.DtsUser;
import com.qf.service.DtsUserService;
import com.qf.vo.DayStatis;
import com.qf.vo.UserOrderCntVo;
import com.qf.vo.UserStatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@Service
public class DtsUserServiceImpl implements DtsUserService {

    @Autowired
    DtsUserMapper userMapper;
    @Autowired
    StatMapper statMapper;

    @Override
    public long getTotal() {
        return userMapper.selectCount(null);
    }

    @Override
    public List<DayStatis> findUserOrderCnt() {
        return statMapper.findUserOrderCnt();
    }

    @Override
    public IPage<DtsUser> queryPage(Integer page, Integer limit, String sort, String order, String username, String mobile) {
        IPage<DtsUser> iPage = new Page<>(page,limit);
        QueryWrapper<DtsUser> qw = new QueryWrapper<>();
        if(!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)){
            if(order.equalsIgnoreCase("desc")){
                qw.orderByDesc(sort);
            }else {
                qw.orderByAsc(sort);
            }
        }
        if(!StringUtils.isEmpty(username)){
            qw.like("username",username);
        }
        if(!StringUtils.isEmpty(mobile)){
            qw.like("mobile",mobile);
        }
        userMapper.selectPage(iPage,qw);
        return iPage;
    }

    @Override
    public void updateByUserLevelAndStatus(int userId) {
        DtsUser user = new DtsUser();
        user.setUpdateTime(new Date());
        user.setUserLevel(2);
        user.setStatus(0);
        user.setId(userId);
        userMapper.updateById(user);
    }

    @Override
    public List<UserStatVo> queryStatUser() {
        return statMapper.queryStatUser();
    }

}
